The Concentration Game
Lab Objective: This is a continuation of the previous lab. Build simple user interfaces in Java and implement event handlers.
In this lab you will improve the concentration game you developed in the last lab.
- Add a second panel to the right of main panel that contains controls that can be used by the user to specify the number of cards desired in the game. For instance, you could use a JTextField to display the number of cards to be used, and two buttons,+ and -, that can be used to increment and decrement the card count, respectively.
- Modify your program so that the manner in which the cards are laid out on the screen changes, in some reasonable way, with the number of cards in the game.
- Add code to modify the card size in relation to the number of cards, i.e. as the number of cards increases, each card shrinks.
- Modify your program to assign randomly generated colors to card pairs.
- Add a menu bar to the game with the menu Game, that has menu items New and Exit. This code details the how-to of menus in Java. A menu item generates action events when selected. Your menu handler should respond to the menu item New by creating a new instance of the concentration game that has the number of cards specified by the user . Selecting Exit does the obvious. Note that setJMenuBar is a method defined in the JFrame class.
- Recall from our discussion in class that the method call gc.actionPerformed(new ActionEvent(c,1,"")) simulates a mouse click on card c. Use this to develop a systematic way of solving an instance of the concentration game.
- Your code should stop when the puzzle is solved, i.e. all cards pairs are revealed.
- Record the number of tries required to solve an instance of the puzzle using your solver.
- Test your game solver for deck sizes of 64, 128, and 256. What can you say about the relationship between the number of tries needed to solve the puzzle and the puzzle size? As in, when the puzzle size doubles, does the number of tries double too? Triple? Quadruple?
- How many tries do you think the systematic approach will require to solve a game with 512 cards? 1024 cards?